package com.hmall.controller.back;

import com.hdmall.business.common.annotation.LoginRequired;

import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.MallAttrValueMapper;
import com.hmall.mapper.MallPriceMapper;
import com.hmall.mapper.SysUserSearchMapper;

import com.hmall.model.*;

import com.hmall.util.TokenUtil;

import com.hmall.vo.PageVo;
import com.hmall.vo.SearchVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/manage/search")
@Api(value = "后台搜索操作API", description = "后台搜索类API说明")
public class SearchManageController {
    private final
    SysUserSearchMapper sysUserSearchMapper;
    private final
    JedisPool jedisPool;

    private TokenUtil tokenUtil = new TokenUtil();

    private final
    MallAttrValueMapper mallAttrValueMapper;
    private final
    MallPriceMapper mallPriceMapper;

    @Autowired
    public SearchManageController(SysUserSearchMapper sysUserSearchMapper, JedisPool jedisPool, MallAttrValueMapper mallAttrValueMapper, MallPriceMapper mallPriceMapper) {
        this.sysUserSearchMapper = sysUserSearchMapper;
        this.jedisPool = jedisPool;
        this.mallAttrValueMapper = mallAttrValueMapper;
        this.mallPriceMapper = mallPriceMapper;
    }

    @LoginRequired
    @PostMapping("CreatedSearch")
    @ApiOperation(value = "添加搜索记录")
    public ServerResponse<String> add(String query, String token)
            throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        Date date = new Date();
        SysUserSearch sysUserSearch = new SysUserSearch();
        sysUserSearch.setContext(query);
        sysUserSearch.setUserId(user.getUserId());

        SysUserSearch sysUserSearch1 = sysUserSearchMapper.selectRepeat(query,
                user.getUserId());

        if (sysUserSearch1 != null) {
            return ServerResponse.createBySuccess("已存在搜索记录");
        }

        sysUserSearch.setCreateTime(date);
        sysUserSearchMapper.insert(sysUserSearch);

        return ServerResponse.createBySuccess("创建成功");
    }

    @LoginRequired
    @DeleteMapping("DeleteSearch")
    @ApiOperation(value = "删除搜索记录")
    public ServerResponse<String> delete(String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();
        sysUserSearchMapper.deleteByUserId(user.getUserId());

        return ServerResponse.createBySuccess("删除成功");
    }

    @LoginRequired
    @GetMapping("SearchList")
    @ApiOperation(value = "搜索记录列表")
    public ServerResponse<PageVo<Map<String, Object>>> list(String token,
                                                            @RequestParam(value = "pageNum", defaultValue = "1")
                                                                    int pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "10")
                                                                    int pageSize) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        int total = sysUserSearchMapper.selectCountData(user.getUserId());

        Integer pageStart = pageSize * (pageNum - 1);

        List<Map<String, Object>> mapList = sysUserSearchMapper.selectByPage(user.getUserId(),
                pageStart, pageSize);

        PageVo<Map<String, Object>> pageVo = new PageVo<>(pageNum, pageSize, total, mapList);

        return ServerResponse.createBySuccess(pageVo);
    }

    @GetMapping("SearchHot")
    @ApiOperation(value = "热搜词")
    public ServerResponse<List<SearchVo>> listHot() {
        List<SearchVo> list = sysUserSearchMapper.selectHot();

        return ServerResponse.createBySuccess(list);
    }

    @GetMapping("Keywords")
    @ApiOperation(value = "搜索关键字")
    public ServerResponse<List<MallAttrValue>> keywords(String query) {
        List<MallAttrValue> list = mallAttrValueMapper.selectKeywords(query);

        return ServerResponse.createBySuccess(list);
    }

    @GetMapping("getProductGrade")
    @ApiOperation(value = "获得产品等级")
    public ServerResponse<String[]> getProductGrade(Integer product_id) {
        String[] result = mallPriceMapper.selectGradeById(product_id);

        return ServerResponse.createBySuccess(result);
    }

    @GetMapping("test")
    @ApiOperation(value = "ceshi1")
    public ServerResponse test() {
        Integer[] list = mallAttrValueMapper.select1();
        for (Integer i : list) {
            mallAttrValueMapper.deleteByPrimaryKey(i);
        }
        return ServerResponse.createBySuccess();
    }

}
